<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>返回最长字符串</title>
  <script>
    //10. 编写函数maxr(char) 返回字符串char中最长的连续重复字母，比如maxr("mmmiijjjjkkkkkkssptr") 返回"kkkkkk"
    function work8(x) {
      //创建两个指针、最长长度、指针位置、最长字符
      var i = 0,
        j = 1,
        maxLength = 0,
        maxLeft = 0,
        macStr = '';
      //指针长度小于字符串长度
      while (i < x.length) {
        //如果两个指针位置字符相同
        if (x[i] === x[j]) {
          //j指针自增
          j++;
          //如果不同
        } else {
          // 判断两个指针间距是否大于maxLength
          if (j - i > maxLength) {
            // 如果大于，重置长度，并且重置字符，记录i指针位置
            maxLength = j - i;
            maxLeft = i;
            macStr = x.charAt(i);
          }
          //i指针指向j的位置
          i = j;
          //j指针自增
          j++;
        }
      }
      //使用substr方法补全字符串
      macStr = x.substr(maxLeft, maxLength);
      //输出最重长复字符串
      return macStr;
    }
    //调用函数
    console.log(work8('mmmiijjjjkkkkkkssptr'));
  </script>
</head>

<body>

</body>

</html>